﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiC3DStepsGrid
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using C1.Win.C1FlexGrid.Util.BaseControls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui48;


namespace DnProcessSimulateCommands;

internal class CUiC3DStepsGrid : TxFlexGridExtension
{
  private const int STEP_NAME_COL_NUM = 0;
  private const int SNAPSHOT_NAME_COL_NUM = 1;
  internal const string ITEM_STYLE = "Item";
  internal const string ITEM_SELECTED_STYLE = "SelectedItem";
  internal const string ITEM_WRONG_SNAPSHOT_STYLE = "WrongSnapshotStyle";
  private ToolTip m_toolTip = new ToolTip();
  private ComboBox m_snapshotsList = new ComboBox();
  private CApC3DManagerApp m_generatorApp;
  private IContainer components;

  internal event CUiC3DStepsGrid.CUiC3DStepsGrid_ListUpdatedEventHandler ListUpdated;

  public CUiC3DStepsGrid() => this.InitializeComponent();

  public CUiC3DStepsGrid(IContainer container)
  {
    container.Add((IComponent) this);
    this.InitializeComponent();
  }

  internal void Initialize(ref CApC3DManagerApp generatorApp)
  {
    this.m_generatorApp = generatorApp;
    this.CreateColumnStyles();
    this.InitializeSnapshotsList();
    this.m_generatorApp.FillAllSteps();
    this.FillStepsList();
  }

  private void CreateColumnStyles()
  {
    ((C1FlexGridBase) this).ShowButtons = (ShowButtonsEnum) 2;
    ((RowCol) ((C1FlexGridBase) this).Cols[0]).AllowEditing = false;
    CellStyle cellStyle1 = ((C1FlexGridBase) this).Styles.Normal;
    cellStyle1 = ((C1FlexGridBase) this).Styles.Add("Item");
    CellStyle cellStyle2 = ((C1FlexGridBase) this).Styles.Add("SelectedItem");
    cellStyle2.BackColor = SystemColors.Highlight;
    cellStyle2.ForeColor = SystemColors.HighlightText;
    ((C1FlexGridBase) this).Styles.Add("WrongSnapshotStyle").BackColor = Color.Red;
  }

  private void FillStepsList()
  {
    foreach (CApC3DProcessNodeData orderedProcess in this.m_generatorApp.GetOrderedProcesses())
    {
      if (orderedProcess.IsStep)
      {
        Row row = ((C1FlexGridBase) this).Rows.Add();
        row[0] = (object) ((ITxObject) orderedProcess.NodeObject).Name;
        orderedProcess.StepsGridRow = row;
        ((RowCol) row).UserData = (object) orderedProcess;
        ((TxFlexGrid) this).SetImageByObject(((RowCol) row).Index, 0, (ITxObject) orderedProcess.NodeObject);
        this.SetRowStyle(row, "Item");
        this.m_snapshotsList.SelectedIndex = 0;
        if (orderedProcess.AttachedSnapshotData != null)
        {
          CApC3DSnapshotNodeData attachedSnapshotData = orderedProcess.AttachedSnapshotData;
          if (attachedSnapshotData.DoesSnapshotExist)
          {
            for (int index = 0; index < this.m_snapshotsList.Items.Count; ++index)
            {
              if (TxSnapshot.op_Equality((this.m_snapshotsList.Items[index] as CApC3DSnapshotComboItem).Snapshot, (ITxObject) attachedSnapshotData.Snapshot))
              {
                this.m_snapshotsList.SelectedIndex = index;
                break;
              }
            }
          }
          else if (attachedSnapshotData.IsCurrentView)
            this.m_snapshotsList.SelectedIndex = 0;
          else if (attachedSnapshotData.IsStartOperationView)
          {
            this.m_snapshotsList.SelectedIndex = 1;
          }
          else
          {
            this.m_snapshotsList.SelectedIndex = -1;
            CellStyle style = ((C1FlexGridBase) this).Styles["WrongSnapshotStyle"];
            ((C1FlexGridBase) this).SetCellStyle(((RowCol) row).Index, 1, style);
          }
        }
        else
        {
          CApC3DSnapshotNodeData dsnapshotNodeData = new CApC3DSnapshotNodeData(this.m_snapshotsList.Items[this.m_snapshotsList.SelectedIndex] as CApC3DSnapshotComboItem);
          orderedProcess.AttachedSnapshotData = dsnapshotNodeData;
        }
        row[1] = (object) orderedProcess.AttachedSnapshotData.ToString();
      }
    }
  }

  private void InitializeSnapshotsList()
  {
    this.m_snapshotsList.DropDownStyle = ComboBoxStyle.DropDownList;
    this.m_snapshotsList.Items.Add((object) new CApC3DSnapshotComboItem(SnapshotType.CurrentView));
    this.m_snapshotsList.Items.Add((object) new CApC3DSnapshotComboItem(SnapshotType.StartOperationView));
    foreach (TxSnapshot snapshot in (IEnumerable<ITxObject>) ((IEnumerable<ITxObject>) this.m_generatorApp.GetAllSnapshots()).OrderBy<ITxObject, string>((Func<ITxObject, string>) (snapshot => ((TxSnapshot) snapshot).Name)))
      this.m_snapshotsList.Items.Add((object) new CApC3DSnapshotComboItem(snapshot));
    ((RowCol) ((C1FlexGridBase) this).Cols[1]).Editor = (Control) this.m_snapshotsList;
    this.m_snapshotsList.DropDownClosed += new EventHandler(this.m_snapshotsList_DropDownClosed);
  }

  internal int StepColumnWidth
  {
    get => ((C1FlexGridBase) this).Cols[0].Width;
    set => ((C1FlexGridBase) this).Cols[0].Width = value;
  }

  internal int SnapshotColumnWidth
  {
    get => ((C1FlexGridBase) this).Cols[1].Width;
    set => ((C1FlexGridBase) this).Cols[1].Width = value;
  }

  internal bool DoesListValid()
  {
    bool flag = ((RowColCollection) ((C1FlexGridBase) this).Rows).Count - ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed > 0;
    if (flag)
    {
      foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
      {
        if (((RowCol) row).Index > 0 && ((RowCol) row).UserData != null && ((RowCol) row).UserData is CApC3DProcessNodeData userData && !userData.AttachedSnapshotData.DoesSnapshotExist)
        {
          flag = false;
          break;
        }
      }
    }
    return flag;
  }

  internal void SetRowStyle(Row row, string style)
  {
    CellStyle style1 = ((C1FlexGridBase) this).Styles[style];
    ((RowCol) row).Style = style1;
  }

  internal void UpdateStepList()
  {
    ((ScrollableControl) this).BeginUpdate();
    this.RemoveAllSteps();
    this.FillStepsList();
    ((ScrollableControl) this).EndUpdate();
  }

  internal ArrayList RemoveSelectedSteps()
  {
    ((ScrollableControl) this).BeginUpdate();
    ArrayList arrayList = new ArrayList();
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows.Selected)
    {
      if (((RowCol) row).Index > 0 && ((RowCol) row).UserData != null)
      {
        if (((RowCol) row).UserData is CApC3DProcessNodeData userData)
          arrayList.Add((object) userData);
        ((C1FlexGridBase) this).Rows.Remove(row);
      }
    }
    ((ScrollableControl) this).EndUpdate();
    return arrayList;
  }

  internal ArrayList RemoveAllSteps()
  {
    ((ScrollableControl) this).BeginUpdate();
    ArrayList arrayList = new ArrayList();
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0 && ((RowCol) row).UserData != null && ((RowCol) row).UserData is CApC3DProcessNodeData userData)
        arrayList.Add((object) userData);
    }
    ((C1FlexGridBase) this).Rows.RemoveRange(((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed, ((RowColCollection) ((C1FlexGridBase) this).Rows).Count - ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed);
    ((ScrollableControl) this).EndUpdate();
    return arrayList;
  }

  public List<CApC3DProcessNodeData> GetSteps()
  {
    List<CApC3DProcessNodeData> steps = new List<CApC3DProcessNodeData>();
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0 && ((RowCol) row).UserData != null && ((RowCol) row).UserData is CApC3DProcessNodeData userData)
        steps.Add(userData);
    }
    return steps;
  }

  private void m_snapshotsList_DropDownClosed(object sender, EventArgs e)
  {
    if (this.m_snapshotsList.SelectedIndex < 0)
      this.m_snapshotsList.SelectedIndex = 0;
    Row row = ((C1FlexGridBase) this).Rows[((C1FlexGridBase) this).RowSel];
    if (((RowCol) row).UserData is CApC3DProcessNodeData userData && userData.AttachedSnapshotData != null && this.m_snapshotsList.SelectedIndex != -1)
    {
      userData.AttachedSnapshotData.Set(this.m_snapshotsList.Items[this.m_snapshotsList.SelectedIndex] as CApC3DSnapshotComboItem);
      CellStyle style = ((C1FlexGridBase) this).Styles["Item"];
      ((C1FlexGridBase) this).SetCellStyle(((RowCol) row).Index, 1, style);
    }
    this.ListUpdated((object) this, new EventArgs());
  }

  private void CUiC3DStepsGrid_MouseMove(object sender, MouseEventArgs e)
  {
    int mouseCol = ((C1FlexGridBase) this).MouseCol;
    int mouseRow = ((C1FlexGridBase) this).MouseRow;
    if (mouseCol == 0 && mouseRow > 0)
    {
      if (((RowCol) ((C1FlexGridBase) this).Rows[((C1FlexGridBase) this).MouseRow]).UserData is CApC3DProcessNodeData userData)
      {
        string fullName = userData.FullName;
        if (!(fullName != this.m_toolTip.GetToolTip((Control) this)))
          return;
        this.m_toolTip.SetToolTip((Control) this, fullName);
      }
      else
        this.m_toolTip.RemoveAll();
    }
    else
      this.m_toolTip.RemoveAll();
  }

  protected virtual void MarkAllSelected()
  {
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows.Selected)
    {
      if (((RowCol) row).Index > 0)
        this.SetRowStyle(row, "SelectedItem");
    }
  }

  protected virtual void ClearAllMarked()
  {
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0)
        this.SetRowStyle(row, "Item");
    }
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    ((TxFlexGrid) this).Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiC3DStepsGrid));
    ((ISupportInitialize) this).BeginInit();
    ((Control) this).SuspendLayout();
    ((C1FlexGridBase) this).AllowSorting = (AllowSortingEnum) 0;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((C1FlexGridBase) this).ExtendLastCol = true;
    ((C1FlexGridBase) this).HighLight = (HighLightEnum) 0;
    ((RowColCollection) ((C1FlexGridBase) this).Rows).Count = 1;
    ((C1FlexGridBase) this).SelectionMode = (SelectionModeEnum) 7;
    ((Control) this).MouseMove += new MouseEventHandler(this.CUiC3DStepsGrid_MouseMove);
    ((ISupportInitialize) this).EndInit();
    ((Control) this).ResumeLayout(false);
  }

  internal delegate void CUiC3DStepsGrid_ListUpdatedEventHandler(object sender, EventArgs args);
}
